package org.springblade.modules.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.service.IEventMessagePushService;
import org.springblade.modules.admin.utils.WeiXinUtil;
import org.springblade.modules.admin.vo.EventMessagePushVO;
import org.springblade.modules.admin.vo.weixin.CommonVO;
import org.springblade.modules.admin.vo.weixin.MessageTemplateVO;
import org.springblade.modules.admin.vo.weixin.MessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Description 上报事件消息推送接口
 * @Author ysq
 * @Date 2021/6/2
 **/
@Slf4j
@Service
public class EventMessagePushServiceImpl implements IEventMessagePushService {

	@Autowired
	private WeiXinUtil weiXinUtil;

	@Async
	@Override
	public void sendWeiXiMessage(EventMessagePushVO message) {
		if (Func.isBlank(message.getOpenid())) {
			return;
		}

		// 封装模板数据
		MessageTemplateVO messageTemplateVO = new MessageTemplateVO();
		messageTemplateVO.setTemplate_id("DQuIDmwQXXGVAOI8-ieKMlC6ralx_mx0immn2_TVYeU");
		MessageVO messageVO = new MessageVO();
		// 标题
		CommonVO first = new CommonVO("电梯警报事件上报",null);
		// 预警类型
		CommonVO keyword1 = new CommonVO("事件警报",null);
		// 预警时间
		CommonVO keyword2 = new CommonVO(message.getTime(),null);
		// 电梯位置
		CommonVO keyword3 = new CommonVO(message.getElevatorAddress(),null);
		// 故障原因
		CommonVO keyword4 = new CommonVO(message.getRemark(),null);
		messageVO.setFirst(first);
		messageVO.setKeyword1(keyword1);
		messageVO.setKeyword2(keyword2);
		messageVO.setKeyword3(keyword3);
		messageVO.setKeyword4(keyword4);
		messageTemplateVO.setData(messageVO);
		String[] openidArr = message.getOpenid().split(",");
		for (String openid : openidArr) {
			messageTemplateVO.setTouser(openid);
			String weiXinResult = weiXinUtil.sendTemplateMessage(JSONObject.toJSONString(messageTemplateVO));
			log.info("-----------------推送公众号消息，openid:{},result:{}----------------",message.getOpenid(),weiXinResult);
		}
	}

	@Async
	@Override
	public void sendSms(EventMessagePushVO message) {
		if (Func.isBlank(message.getPhone())) {
			return;
		}

		String[] phoneArr = message.getPhone().split(",");
		for (String phone : phoneArr) {
			log.info("-----------------发送手机短信，phone:{}----------------",phone);
		}
	}
}
